package com.ui
{
	import com.engine.AwayEngine;
	import com.engine.PowerEngine;
	import com.engine.SurfaceResize;
	import com.greensock.TweenLite;
	import com.manager.CarMaterial;
	import com.manager.Vision;
	import com.vo.TypeVo;
	
	import flash.geom.Point;
	
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.ResizeEvent;
	import starling.events.Touch;
	import starling.events.TouchPhase;
	import starling.text.TextField;
	
	/**
	 * 左边显示车系的数据ui界面
	 */	
	public class TypeFace extends SurfaceResize
	{
		private static var menu:TypeFace;
		public static function getInstance():TypeFace{
			if(menu== null) menu = new TypeFace();
			return menu;
		}
		
		private var contain:Sprite;
		private var typeDes:TextField;
		private var pallet:Image;
		private var isOpen:Boolean;//是否展开绘图板
		
		public function show():void{
			if(contain == null){
				contain = new Sprite();
				var image:Image = Vision.introduce(183,1,0,47.5,0x2b2c32);
				//				contain.addChild(image);
				image.height = 300;
				
				contain.addEventListener(Event.ADDED_TO_STAGE,addToStage);
				createCarChoiceColor();
				createColorBtn();
				createPallet();
				
				typeDes = Vision.createText('',0xCFCFCF,15,70,160,260);
				//				contain.addChild(typeDes);
				//				contain.x = 1180;
			}
			Vision.addView(Vision.UI,contain);
			
		}
		private function addToStage(e:Event):void
		{
			contain.removeEventListener(ResizeEvent.RESIZE,addToStage);
			resize();
		}
		protected override function resize():void
		{
			contain.x = AwayEngine.senceWidth/2 +450;
			contain.y = 35;
		}
		private function createPallet():void{
			pallet = Image.fromBitmap(new CustomSkin.colorSetsClass());
			pallet.y = 10;
			pallet.x = 82;
			pallet.scaleX = 0;
			pallet.scaleY = 0;
			contain.addChild(pallet);
			pallet.addEventListener(starling.events.TouchEvent.TOUCH,onColorImage); 
			contain.addEventListener(starling.events.TouchEvent.TOUCH,onTouchSets);
		}
		
		private function onTouchSets(e:starling.events.TouchEvent):void{
			var touch:Touch = e.getTouch(e.target as Image);
			if(touch!=null){
				if(touch.phase == "began"){
					if(isOpen == false){
						TweenLite.to(pallet,.5,{scaleX:1,scaleY:1});
						isOpen = true;
					}
					else{
						TweenLite.to(pallet,.5,{scaleX:0,scaleY:0});
						isOpen = false;
					}
				}
			}
		}
		
		private function onColorImage(e:starling.events.TouchEvent):void{
			var touch:Touch = e.getTouch(e.target as Image);
			if(touch!=null){
				if(touch.phase == TouchPhase.BEGAN){
					var point:Point = pallet.globalToLocal(new Point(touch.globalX,touch.globalY));
					var colosTouch:Number = CustomSkin.colorSetsBd.getPixel(point.x,point.y);
					if(colosTouch != 0){
						CarMaterial.changeColor(colosTouch);
						//						trace(colosTouch)
						//						StageBackground.getInstance().show(colosTouch);
						//						PowerEngine.backClass = StageBackground;
					}
				}
			}else{
				TweenLite.to(pallet,.5,{scaleX:0,scaleY:0});
				isOpen = false;
			}
		}
		
		protected function createColorBtn():void{
			CarMaterial.changeColor(0x0099ff);
		}
		
		private function createCarChoiceColor():void{
			var carChoiceColorImage:Image = CustomSkin.getImage("CarChoiceColor");
			carChoiceColorImage.alpha = 1;
			carChoiceColorImage.width = 150;
//			carChoiceColorImage.height = 35;
			contain.addChildAt(carChoiceColorImage,0);
//						contain.addChild(carChoiceColorImage);
		}
		
		private function onBtnImage(e:starling.events.TouchEvent):void{
			var touch:Touch = e.getTouch(e.target as Image);
			if(touch!=null){
				if(touch.phase == TouchPhase.BEGAN){
					var colosTouch:uint = uint((e.target as Image).name);
					CarMaterial.changeColor(colosTouch);
				}
			}
		}
		
		public function showType(tvo:TypeVo):void{
			typeDes.text = tvo.des;
		}
		
	}
}
